package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.PurchaseRequest;
import com.amsp.oa.oademo.service.PurchaseRequestService;
import com.amsp.oa.oademo.vo.PurchaseRequestVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 申购单Controller
 */
@Api(tags = "申购单管理")
@RestController
@RequestMapping("/api/purchase-request")
public class PurchaseRequestController {

    @Resource
    private PurchaseRequestService purchaseRequestService;

    @ApiOperation("获取申购单列表")
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getList(
            @ApiParam(value = "页码", required = true) @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页大小", required = true) @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "月份，格式：yyyy-MM") @RequestParam(required = false) String month,
            @ApiParam(value = "部门") @RequestParam(required = false) String department) {

        IPage<PurchaseRequest> page = purchaseRequestService.getList(pageNum, pageSize, month, department);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "获取成功");

        Map<String, Object> data = new HashMap<>();
        data.put("records", page.getRecords());
        data.put("total", page.getTotal());
        data.put("size", page.getSize());
        data.put("current", page.getCurrent());
        data.put("pages", page.getPages());

        result.put("data", data);

        return ResponseEntity.ok(result);
    }

    @ApiOperation("获取申购单详情")
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getDetail(
            @ApiParam(value = "申购单ID", required = true) @PathVariable Long id) {

        PurchaseRequestVO detail = purchaseRequestService.getDetail(id);

        Map<String, Object> result = new HashMap<>();
        if (detail != null) {
            result.put("code", 200);
            result.put("message", "获取成功");
            result.put("data", detail);
        } else {
            result.put("code", 404);
            result.put("message", "申购单不存在");
        }

        return ResponseEntity.ok(result);
    }

    @ApiOperation("保存或更新申购单")
    @PostMapping("/save")
    public ResponseEntity<Map<String, Object>> save(@RequestBody PurchaseRequestVO purchaseRequestVO) {
        boolean success = purchaseRequestService.saveOrUpdate(purchaseRequestVO);

        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "保存成功");
        } else {
            result.put("code", 500);
            result.put("message", "保存失败");
        }

        return ResponseEntity.ok(result);
    }

    @ApiOperation("删除申购单")
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(
            @ApiParam(value = "申购单ID", required = true) @PathVariable Long id) {

        boolean success = purchaseRequestService.delete(id);

        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "删除失败");
        }

        return ResponseEntity.ok(result);
    }

    @ApiOperation("批量删除申购单")
    @PostMapping("/batch-delete")
    public ResponseEntity<Map<String, Object>> batchDelete(@RequestBody List<Long> ids) {
        boolean success = purchaseRequestService.batchDelete(ids);

        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "批量删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "批量删除失败");
        }

        return ResponseEntity.ok(result);
    }
}